﻿#include "mergetool.h"

#include <QGraphicsItem>
#include <QGraphicsSceneContextMenuEvent>
#include <QGraphicsSceneMouseEvent>
#include <QList>
#include <QMenu>

#include "../core/floor.h"
#include "../core/polygonfeature.h"
#include "../core/room.h"
#include "../core/scene.h"
#include "../gui/documentview.h"

#if defined(Q_CC_MSVC)
#pragma execution_character_set("utf-8")
#endif

MergeTool::MergeTool(DocumentView *parent) : AbstractTool(parent) {}

void MergeTool::mousePressEvent(QGraphicsSceneMouseEvent *event) {
  event->setModifiers(Qt::ControlModifier);
}

void MergeTool::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
  //    Scene *scene = m_doc->scene();
  //    QGraphicsItem* curItem = scene->itemAt(event->scenePos().x(),
  //    event->scenePos().y(), QTransform()); if(curItem == NULL){
  //        m_selectedList.clear();
  //        scene->clearSelection();
  //    }else{
  //        foreach(QGraphicsItem* item, m_selectedList){
  //            item->setSelected(true);
  //        }
  //        curItem->setSelected(!item->isSelected());
  //        if(!curItem->isSelected()){
  //            m_selectedList.removeOne(curItem);
  //        }else{
  //            m_selectedList.append(curItem);
  //        }
  //    }
  event->setModifiers(Qt::ControlModifier);
  // foreach(GraphicsItem* item, m_selectedList)
}

void MergeTool::contextMenuEvent(QGraphicsSceneContextMenuEvent *event) {
  Scene *scene = m_doc->scene();
  QList<QGraphicsItem *> itemList = scene->selectedItems();
  if (itemList.size() > 1) {
    if (m_contextMenu == NULL) {
      m_contextMenu = new QMenu();
      QAction *mergeAction = m_contextMenu->addAction("合并");
      connect(mergeAction, SIGNAL(triggered()), this,
              SLOT(mergeSelectedItems()));
    }
    m_contextMenu->exec(event->screenPos());
  }
}

void MergeTool::mergeSelectedItems() {
  Scene *scene = m_doc->scene();
  QList<QGraphicsItem *> itemList = scene->selectedItems();
  QVector<QPolygonF> polyArray;
  Floor *tmpFloor = NULL;

  // get all the polygons
  foreach (QGraphicsItem *item, itemList) {
    PolygonFeature *poly = dynamic_cast<PolygonFeature *>(item);
    if (poly != NULL) {
      polyArray.push_back(poly->outline());
      if (poly->isClassOf("Floor")) {
        tmpFloor = dynamic_cast<Floor *>(poly);
      }
    }
  }

  // merge the polygons
  if (polyArray.size() >= 2) {
    QPolygonF polygon = merge(polyArray);
    if (tmpFloor) {
      tmpFloor->setOutline(polygon);
    } else {
      Room *newArea = new Room("未命名", polygon);
      newArea->computeArea();
      newArea->computeCenter();
      newArea->setParentFeature(scene->currentFloor());
    }
  }

  // delte the old funcAreas
  foreach (QGraphicsItem *item, itemList) {
    Room *room = dynamic_cast<Room *>(item);
    if (room != NULL) {
      scene->deleteMapFeature(room);
      room = NULL;
    }
  }
  scene->clearSelection();
}

QPolygonF MergeTool::merge(QVector<QPolygonF> &polygons) {
  if (polygons.size() > 2) {
    while (polygons.size() > 2) {
      QVector<QPolygonF> twoPoly;
      twoPoly.push_back(polygons.last());
      polygons.pop_back();
      twoPoly.push_back(polygons.last());
      polygons.pop_back();
      QPolygonF tmpPoly = merge(twoPoly);
      polygons.push_back(tmpPoly);
    }
  } else if (polygons.size() == 2) {
    return polygons[0].united(polygons[1]);
  } else if (polygons.size() == 1) {
    return polygons[0];
  }

  // else
  return QPolygonF();
}
